package dome5;

import java.util.Arrays;

public class MyArrayList implements IList {
    private int[] elem;
    private int usedSize;

    public MyArrayList() {
        this.elem = new int[10];
        this.usedSize = 0;
    }
    @Override
    public boolean isFull() {
        return usedSize == elem.length;
    }
    @Override
    public void add(int data) {
        if(isFull()) {
            resize();
        }
        elem[usedSize] = data;
        usedSize++;
    }
    private void resize() {
        elem = Arrays.copyOf(elem,2*elem.length);
    }
    @Override
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i] + " ");
        }
        System.out.println();
    }
    @Override
    public void add(int pos, int data) {
        if (pos < 0 || pos > usedSize) {
            throw new PosOutOfException("pos位置不合法!");
        }
        if (isFull()) {
            resize();
        }
        for (int i = usedSize; i > pos ; i--) {
            //elem[pos+1] = elem[pos];
            elem[i] = elem[i-1];
        }
        elem[pos] = data;
        usedSize++;
    }
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }
    @Override
    public int get(int pos) {
        if(pos < 0 || pos > usedSize) {
            throw new PosOutOfException("pos位置不合法！");
        }
        if(isEmpty()) {
            return -1;
        }
        return elem[pos];
    }
    private boolean isEmpty() {
        return usedSize == 0;
    }
    @Override
    public void set(int pos, int value) {
        if(pos < 0 || pos > usedSize) {
            throw new PosOutOfException("pos位置不合法！");
        }
        elem[pos] = value;
    }
    @Override
    public void remove(int toRemove) {
//        for (int i = 0; i < usedSize; i++) {
//            if(elem[i] == toRemove) {
//                for (int j = i; j < usedSize - 1; j++) {
//                    //elem[usedSize-2] = elem[usedSize-1];
//                    elem[j] = elem[j+1];
//                }
//                usedSize--;
//                return;
//            }
//        }
//        System.out.println("没有你要删除的元素!");
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有你要删除的元素!");
            return;
        }
        for (int i = index; i < usedSize - 1; i++) {
                    //elem[usedSize-2] = elem[usedSize-1];
                    elem[i] = elem[i+1];
                }
                usedSize--;
    }
    @Override
    public int size() {
        return usedSize;
    }
    @Override
    public void clear() {
        elem = null;
        usedSize = 0;
    }
}
